package com.sxygsj.baselibrary.network.netUtils

import com.sxygsj.baselibrary.network.UploadFileRequestBody
import com.sxygsj.baselibrary.network.UploadWork
import com.sxygsj.baselibrary.network.netInterface.ProgressInterface
import com.sxygsj.baselibrary.network.netInterface.ProgressObserver
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MultipartBody
import java.io.File
import java.net.URLConnection
import java.net.URLEncoder

/**
 * @Author LD
 * @Time 2021/3/4 16:28
 * @Describe 上传文件工具类
 * @Modify
 */
object UploadUtil {

    private var job = Job()

    /**
     * 上传一个文件
     */
    fun uploadFile(url:String,filePath: String,progressInterface: ProgressInterface) {
        var file = File(filePath)
        //获取文件的格式
        var type = URLConnection.getFileNameMap().getContentTypeFor(filePath)

        //※※※观察RequestBody内的进度
        var progressObserver = object :
            ProgressObserver {
            override fun onProgress(progress: Int) {
               progressInterface.onProgress(progress)
            }
        }
        var requestBody = UploadFileRequestBody(
            MediaType.parse(type), file,
            progressObserver
        )


        var name = URLEncoder.encode(file.name, "UTF-8")
        // TODO: 2021/3/8 这里的uploadFile是和后端约定的，具体项目再更改
        val multipartBody = MultipartBody.Part.createFormData("uploadFile", name, requestBody)
        val scope = CoroutineScope(job)

        scope.launch {
            try {
                var responseBody = UploadWork.uploadFile(url,multipartBody)
                //上传成功
                progressInterface.onSuccess(responseBody)

            } catch (e: Exception) {
                //上传失败
                progressInterface.onFailed(e.message.toString())
            }
        }

    }


    /**
     * 上传多个文件
     */
    fun uploadFiles(url:String,filePathList: List<String>,progressInterface: ProgressInterface){
        var list = mutableListOf<MultipartBody.Part>()
        var progressObserver = object :
            ProgressObserver {
            override fun onProgress(progress: Int) {
                progressInterface.onProgress(progress)
            }
        }
        for (filePath in filePathList) {

            var file = File(filePath)
            var type = URLConnection.getFileNameMap().getContentTypeFor(filePath)
            var requestBody =
                UploadFileRequestBody(
                    MediaType.parse(type), file,
                    progressObserver
                )
            var name = URLEncoder.encode(file.name, "UTF-8")
            val multipartBody = MultipartBody.Part.createFormData("uploadFile", name, requestBody)
            list.add(multipartBody)
        }
        val scope = CoroutineScope(job)
        scope.launch {
            try {
                //上传成功
                var responseBody = UploadWork.uploadFiles(url,list)
                progressInterface.onSuccess(responseBody)
            } catch (e: Exception) {
                //上传失败
                progressInterface.onFailed(e.message.toString())
            }
        }
    }
}